<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/1/5
 * Time: 16:52
 */

namespace YMApi\Service;

/**
 * Class BuyNowNoticeService
 * @package SDXApi\Service
 */
class BuyNowNoticeService
{
	/**
	 * @var \Model|\Think\Model
	 */
	protected static $buyNowNoticeModel;

	/**
	 * @var \Model|\Think\Model
	 */
	protected static $goodsModel;

	/**
	 * @var \Model|\Think\Model
	 */
	protected static $userModel;

	/**
	 * @var
	 */
	protected $noticeError;

	/**
	 * @var \Controller|false|\Think\Controller
	 */
	protected static $smsApi;

	/**
	 * 实例注入
	 * BuyNowNoticeService constructor.
	 */
	public function __construct()
	{
		self::$buyNowNoticeModel = D('BuyNowNotice');
		self::$goodsModel = D('Goods');
		self::$userModel = D('Users');
		self::$smsApi = A('Smsapi');
	}

	/**
	 *  订阅通知
	 * Created by Jerry
	 * @param $data
	 * @return $this
	 */
	public function storeNotice($data)
	{
		if (empty($data)) {
			$this->noticeError = '订阅失败';
		}

		$isNotice = $this->isNotice($data['user_id'], $data['goods_id']);

		if ($isNotice === false) {
			$this->noticeError = '订阅失败';
		}

		$isMobile = self::isUserMobile($data['user_id']);

		if (!$isMobile) {
			$this->noticeError = '请绑定手机号码';
		}

		if ($isNotice == 0 && $isMobile) {
			$isNotice = self::$buyNowNoticeModel->store($data);
		}

		return $this;

	}

	/**
	 * 获取通知错误
	 * Created by Jerry
	 * @return mixed
	 */
	public function getNoticeError()
	{
		return $this->noticeError;
	}

	/**
	 * 判断是否版定手机
	 * Created by Jerry
	 * @param $user_id
	 * @return bool
	 */
	static protected function isUserMobile($user_id)
	{
		if (empty($user_id)) {
			return false;
		}

		$mobile = self::$userModel->findBuyNowNoticeUserMobile($user_id);

		if (empty($mobile)) {
			return false;
		}

		return true;
	}

	/**
	 * 判断是否已订阅通知
	 * Created by Jerry
	 * @param $user_id
	 * @param $goods_id
	 * @return bool
	 */
	public function isNotice($user_id, $goods_id)
	{
		if (empty($user_id) || empty($goods_id)) {
			return false;
		}

		return self::$buyNowNoticeModel->isNotice($user_id, $goods_id);
	}

	/**
	 * 处理通知短信
	 * Created by Jerry
	 * @return bool
	 */
	public function handNoticeSendSms()
	{
		$notices = $this->findBuyNowNoticeAll();

		if (empty($notices)) {
			return false;
		}

		$noticesData = $this->checkNoticeData($notices);

		return $noticesData;
	}

	/**
	 * Created by Jerry
	 * @return mixed
	 */
	protected function findBuyNowNoticeAll()
	{
		return self::$buyNowNoticeModel->findBuyNowNoticeList();
	}

	/**
	 * 处理通知数据
	 * Created by Jerry
	 * @param $notices
	 * @return bool
	 */
	protected function checkNoticeData($notices)
	{
		if (empty($notices)) {
			return false;
		}

		foreach ($notices as $key => $value) {

			$goodsInfo = $this->findGoodsInfo($value['goods_id']);

			$userInfo = $this->findUserInfo($value['user_id']);

			if (empty($goodsInfo) || empty($userInfo)) {
				continue;
			}

			if (empty($userInfo['consignee'])) {
				$userInfo['consignee'] = '食得鲜会员';
			}

			$isTime = $this->isNoticeTime($goodsInfo);

			if ($isTime == true) {
				$this->sendSms($userInfo, $goodsInfo, $value['id']);
				$notices[$key]['goodsInfo'] = $goodsInfo;
			}
		}

		return $notices;
	}

	/**
	 * 返回商品详情
	 * Created by Jerry
	 * @param $goods_id
	 * @return bool
	 */
	protected function findGoodsInfo($goods_id)
	{
		if (empty($goods_id)) {
			return false;
		}

		return self::$goodsModel->findBuyNowGoodsInfoByGoodsId($goods_id);
	}

	/**
	 * 返回会员详情
	 * Created by Jerry
	 * @param $user_id
	 * @return mixed
	 */
	protected function findUserInfo($user_id)
	{
		return self::$userModel->findBuyNowUserInfo($user_id);
	}

	/**
	 * 判断是否差5分钟开场
	 * Created by Jerry
	 * @param $notice
	 * @return bool
	 */
	protected function isNoticeTime($notice)
	{
		$time = gmtime();
		$noticeTime = $notice['promote_start_date'] - $time;

		if ($noticeTime <= 300 && $noticeTime > 0) {
			return true;
		}

		return false;
	}

	/**
	 * 发送短信
	 * Created by Jerry
	 * @param $userInfo
	 * @param $goodsInfo
	 * @param $notice_id
	 * @return mixed
	 */
	protected function sendSms($userInfo, $goodsInfo, $notice_id)
	{
		$rs = self::$smsApi->send_message_api('buy_now_notice', $userInfo['mobile_phone'], $userInfo['consignee'], $userInfo['user_rank'], 0, 0, $userInfo['user_id'], $goodsInfo);

		if ($rs) {
			$this->updateNoticeStatus($notice_id);
		}

		return $rs;
	}

	/**
	 * Created by Jerry
	 * @param $notice_id
	 */
	protected function updateNoticeStatus($notice_id)
	{
		return self::$buyNowNoticeModel->updateBuyNowNoticeStatus($notice_id);
	}
}